<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/page_other.php' );

if( !in_array( "N", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

if( $_SERVER['REQUEST_METHOD'] == 'GET'){	
	
	$forms = $_GET;
	$category1 = $_GET["category1"];
	$category2 = $_GET["category2"];
	$category3 = $_GET["category3"];
	$category4 = $_GET["category4"];
	$name = $_GET["name"];
	$kana = $_GET["kana"];
	$email = $_GET["email"];
	$phone = $_GET["phone"];
	$delivery_date_1 = $_GET["delivery_date_1"];
	$delivery_date_2 = $_GET["delivery_date_2"];
	$m_date1 = $_GET["m_date_1"];
	$m_date2 = $_GET["m_date_2"];
	$pay_type1 = $_GET["pay_type1"];
	$pay_type2 = $_GET["pay_type2"];
	$pay_type3 = $_GET["pay_type3"];
	$pay_type4 = $_GET["pay_type4"];
	$o_status1 = $_GET["o_status1"];
	$o_status2 = $_GET["o_status2"];
	$m_status1 = $_GET["m_status1"];
	$m_status2 = $_GET["m_status2"];
	$delivery_order1 = $_GET["delivery_order1"];
	$delivery_order2 = $_GET["delivery_order2"];
	$email_status1 = $_GET["email_status1"];
	$email_status2 = $_GET["email_status2"];
	$sale_status1 = $_GET["sale_status1"];
	$sale_status2 = $_GET["sale_status2"];
	
	$current=1;
	if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){
		$current = $_GET['page'];
		$st = ($current -1) * PAGE_SIZE;
	}else{
		$current = 1;
		$st = 0;
	}
	
	$where = '';
	$query = '';
	$category = array();
	if($category1 != null){
		$category[] = $category1;	
		$query .="&category1=".$category1;
	}
	if($category2 != null){
		$category[] = $category2;
		$query .="&category2=".$category2;
	}
	if($category3 != null){
		$category[] = $category3;
		$query .="&category3=".$category3;
	}
	if($category4 != null){
		$category[] = $category4;
		$query .="&category4=".$category4;
	}
	if(count($category)>0){
		$where = $where." and category in (".implode(",", $category).")";
	}
	
	if($name != null){
		$where = $where." and name like '%".$name."%'";	
		$query .="&name=".$name;
	}
	if($kana != null){
		$where = $where." and kana like '%".$kana."%'";
		$query .="&kana=".$kana;
	}
	if($email != null){
		$where = $where." and email like '%".$email."%'";	
		$query .="&email=".$email;
	}
	
	if($phone != null){
		$where = $where." and phone like '%".$phone."%'";
		$query .="&phone=".$phone;
	}
	if($delivery_date_1 != null && $delivery_date_2 != null){
		$where = $where." and delivery_date BETWEEN '".$delivery_date_1."' and '".$delivery_date_2."'";
		$query .="&delivery_date_1=".$delivery_date_1."&delivery_date_2=".$delivery_date_2;
	}
	if($delivery_date_1 != null && $delivery_date_2 == null){
		$where = $where." and delivery_date >= '".$delivery_date_1."'";
		$query .="&delivery_date_1=".$delivery_date_1;
	}
	if($delivery_date_1 == null && $delivery_date_2 != null){
		$where = $where." and delivery_date <= '".$delivery_date_2."'";
		$query .="&delivery_date_2=".$delivery_date_2;
	}
	
	if($m_date1 != null && $m_date2 != null){
		$where = $where." and m_date BETWEEN '".$m_date1."' and '".$m_date2."'";	
		$query .="&u_date1=".$m_date1."&u_date2=".$m_date2;
	}
	
	$pay_type = array();
	if($pay_type1 != null){
		$pay_type[] = $pay_type1;
		$query .="&pay_type1=".$pay_type1;
	}
	if($pay_type2 != null){
		$pay_type[] = $pay_type2;
		$query .="&pay_type2=".$pay_type2;
	}
	if($pay_type3 != null){
		$pay_type[] = $pay_type3;
		$query .="&pay_type3=".$pay_type3;
	}
	if($pay_type4 != null){
		$pay_type[] = $pay_type4;
		$query .="&pay_type4=".$pay_type4;
	}
	if(count($pay_type)>0){
		$where = $where." and pay_type in (".implode(",", $pay_type).")";
	}
	
	$o_status = array();
	if($o_status1 != null){
		$o_status[] = $o_status1;
		$query .="&o_status1=".$o_status1;
	}
	if($o_status2 != null){
		$o_status[] = $o_status2;
		$query .="&o_status2=".$o_status2;
	}
	if(count($o_status)>0){
		$where = $where." and o_status in (".implode(",", $o_status).")";
	}
	
	$m_status = array();
	if($m_status1 != null){
		$m_status[] = $m_status1;
		$query .="&m_status1=".$m_status1;
	}
	if($m_status2 != null){
		$m_status[] = $m_status2;
		$query .="&m_status2=".$m_status2;
	}
	if(count($m_status)>0){
		$where = $where." and m_status in (".implode(",", $m_status).")";
	}
	
	$delivery_order = array();
	if($delivery_order1 != null){
		$delivery_order[] = $delivery_order1;
		$query .="&delivery_order1=".$delivery_order1;
	}
	if($delivery_order2 != null){
		$delivery_order[] = $delivery_order2;
		$query .="&delivery_order2=".$delivery_order2;
	}
	if(count($delivery_order)>0){
		$where = $where." and delivery_order in (".implode(",", $delivery_order).")";
	}
	
	$email_status = array();
	if($email_status1 != null){
		$email_status[] = $email_status1;
		$query .="&email_status1=".$email_status1;
	}
	if($email_status2 != null){
		$email_status[] = $email_status2;
		$query .="&email_status2=".$email_status2;
	}
	if(count($email_status)>0){
		$where = $where." and email_status in (".implode(",", $email_status).")";
	}
	
	$sale_status = array();
	if($sale_status1 != null){
		$sale_status[] = $sale_status1;
		$query .="&sale_status1=".$sale_status1;
	}
	if($sale_status2 != null){
		$sale_status[] = $sale_status2;
		$query .="&sale_status2=".$sale_status2;
	}
	if(count($sale_status)>0){
		$where = $where." and sale_status in (".implode(",", $sale_status).")";
	}
	
	$sql =  "SELECT * FROM k_order_head WHERE 1 = 1 $where order by order_id desc LIMIT $st, ".PAGE_SIZE;
	$sqlcount ="SELECT COUNT(*) as cnt FROM k_order_head WHERE 1=1 ".$where;
	$db = new Class_DB;		
	$res = $db->query($sql);
	
	while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
		if($row['category']==1){
			$row['category'] = "プリント工房PRO";
		}else if($row['category']==2){
			$row['category']= "プリント工房LITE";
		}else if($row['category']==3){
			$row['category']= "名刺工房";
		}else if($row['category']==4){
			$row['category']= "アルバム工房";
		}
		$row['m_date']=date('Y/m/d',strtotime($row['m_date']));
		$row['delivery_date']=$row['delivery_date']==NULL?"":date('Y/m/d',strtotime($row['delivery_date']));
		
		$list[] = $row;
	}
	
	$cnt = 0;
	$db->query( $sqlcount );
	$row = $db->fetch();
	$cnt = $row['cnt'];
	
	$smarty->assign('page', getPage($current, $cnt,$query));
	if( isset( $forms ) ) $smarty->assign('forms', $forms);
}



$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );

$smarty->assign('menu_html', $menu_html);

$smarty->assign('cnt', $cnt);
$smarty->assign('list', $list);

$smarty->display('web-admin/order_search.html');


?>